package ui.panels
{
	import events.GestureEvent;
	
	import flash.text.TextField;
	
	import global.Managers;
	
	import lang.LanguageManager;
	
	import logics.DishManager;
	
	import resources.Resources;
	
	import ui.components.Button;
	import ui.components.renderlist.RenderList;
	import ui.layout.renderer.vo.OrderedDishRendererVO;
	import ui.prompt.PromptBox;
	
	import utils.TextUtil;
	
	import vo.BillDishVO;
	import vo.BillVO;
	import vo.DishVO;
	
	public class OrderedDishPanel extends AbstractPanel
	{
		private var _renderList					: RenderList;
		private var _txtTotalPrice				: TextField;
		private var _btnApplyPay				: Button;
		private var _btnConfirm					: Button;
		private var _btnOK						: Button;
		
		public function OrderedDishPanel()
		{
			super(PanelType.ORDERED_DISH);
			
			_bg.bitmapData = Managers.resourceManager.getBitmapData(Resources.PANEL_RECIPE_DETAIL_BG);
			
			_renderList = new RenderList();
			_renderList.itemNumHorizon = 1;
			_renderList.itemNumVerticle = 10;
			_renderList.x = 50;
			_renderList.y = 10;
			addChild(_renderList);
			
			_txtTotalPrice = TextUtil.getTextFieldEasy(" ");
			_txtTotalPrice.x = 500;
			_txtTotalPrice.y = 50;
			addChild(_txtTotalPrice);
			
			_btnApplyPay = new Button(1, "", 150, 40, "", function(event:GestureEvent):void {
				PromptBox.confirm(Managers.languageManager.getLanguageText("bill_apply_pay_attention"), "",
					function():void {
						Managers.billManager.applyPayBill();
					}
				);
			});
			_btnApplyPay.x = this.width - 160;
			_btnApplyPay.y = this.height - 160;
			_btnApplyPay.addListener(this);
			addChild(_btnApplyPay);
			
			_btnConfirm = new Button(2, "", 150, 40, "", function(event:GestureEvent):void {
				PromptBox.confirm(Managers.languageManager.getLanguageText("bill_confirm_attention"), "",
					function():void {
						Managers.billManager.sendBill();
					}
				);
			});
			_btnConfirm.x = this.width - 160;
			_btnConfirm.y = this.height - 110;
			_btnConfirm.addListener(this);
			addChild(_btnConfirm);
			
			_btnOK = new Button(3, "", 150, 40, "", function(event:GestureEvent):void {
				Managers.panelManager.close(_type);
			});
			_btnOK.x = this.width - 160;
			_btnOK.y = this.height - 60;
			_btnOK.addListener(this);
			addChild(_btnOK);
		}
		private function updateView():void {
			var billVO:BillVO = Managers.billManager.billVO;
			var dishManager:DishManager = Managers.dishManager;
			var languageManager:LanguageManager = Managers.languageManager;
			
			var dishes:Array = billVO.dishes;
			var tempDishes:Array = billVO.tempDishes;
			var values:Array = [];
			var price:int = 0;
			var billDishVO:BillDishVO;
			for each(billDishVO in dishes) {
				var value:OrderedDishRendererVO = new OrderedDishRendererVO();
				var dishVO:DishVO = dishManager.getValueByID(billDishVO.dishID);
				value.dishID = billDishVO.dishID;
				value.status = billDishVO.status;
				value.confirmed = true;
				values.push(value)
				price += dishVO.price;
			}
			for each(billDishVO in tempDishes) {
				value = new OrderedDishRendererVO();
				dishVO = dishManager.getValueByID(billDishVO.dishID);
				value.dishID = billDishVO.dishID;
				value.status = billDishVO.status;
				value.confirmed = false;
				values.push(value);
				price += dishVO.price;
			}
			values.sortOn("dishID");
			_renderList.setDataProvider(values);
			_txtTotalPrice.text = languageManager.getLanguageText("total_price") + ": " + price + languageManager.getLanguageText("yuan");
			_btnApplyPay.text = Managers.languageManager.getLanguageText("pay_bill");
			_btnConfirm.text = Managers.languageManager.getLanguageText("confirm_bill");
			_btnOK.text = Managers.languageManager.getLanguageText("ok");
		}
		public function update():void {
			updateView();
		}
		
		override public function openHandler():void {
			super.openHandler();
			update();
		}
		override public function switchLanguage(toLanguage:int):void {
			super.switchLanguage(toLanguage);
			
			if(toLanguage == _currentLanguage) return;
			_currentLanguage = toLanguage;
			var manager:LanguageManager = Managers.languageManager
		}
	}
}